var ctx=window.daijia.ctx;
var map = null; //地图对象

var offset=0;
var limit=20;
var pagingLimit=20;//显示分页时的条数
var currentTotalElements=0;//当前显示总条数


var pageLoading=true;//是否能滚动加载
var hasShowPages=false;//是否已显示分页
var firstLoadPages=true;//分页初始化时 不自动加载

//筛选条件
var search_orderNumber=null;
var search_driverPhone=null;
var search_startTime=null;
var search_endTime=null;
var search_orderType=[];

$(document).ready(function(){
	
	$("#startTime").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true });
	$("#endTime").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true });
	
	initdata(false);
	
	//点击选择时间
	$("#dateRangeButton").click(function(){
		var dropdowncalendar=$(this).parent().find(".dropdown-calendar");
		if(dropdowncalendar.is(':hidden')){
			dropdowncalendar.show();
			if(dropdowncalendar.find(".datepicker").length==0){
				$('#dateRangeCalendar').DatePicker({
					flat: true,
					current: new Date(),
					date: [new Date()],
					calendars: 3,
					mode: 'range',
					starts: 1,
					onChange: function(formated, dates){
						$("#startTime").val(formated[0]);
						$("#endTime").val(formated[1]);
					}
				});
			}
		}
		else{
			dropdowncalendar.hide();
		}
	});
	
	//点击应用
	$("#applyIt").click(function(){
		$("#dateRangeText").text($("#startTime").val()+" 至 "+$("#endTime").val());
		$("#dateRangeButton").parent().find(".dropdown-calendar").hide();
		$("#searchButton").click();
	});
	
	//点击清空
	$("#resetIt").click(function(){
		$("#startTime").val("");
		$("#endTime").val("");
		$("#dateRangeText").text("选择时间");
		$("#dateRangeButton").parent().find(".dropdown-calendar").hide();
		$("#searchButton").click();
	});
	
	//高级搜索
	$(".query-jiant").click(function(){
		var src=$("#jiant").attr("src");
		if(src.indexOf("jiant2")>=0){
			$("#jiant").attr("src",ctx+"/assets/img/jiant.png");
			$(".choose").slideDown(300);
		}else{
			$("#jiant").attr("src",ctx+"/assets/img/jiant2.png");
			$(".choose").slideUp(300);
		}
	});
	
	//查询
	$("#searchButton").click(function(){
		
		var DATE_FORMAT = /^[0-9]{4}-[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1}$/;
		if($("#startTime").val()!=""){
			if(!DATE_FORMAT.test($("#startTime").val())){
			   $.scojs_message("您输入的启始日期格式有误，正确格式如：2012-01-01", $.scojs_message.TYPE_ERROR);
			   return false;
			}
		}
		
		if($("#endTime").val()!=""){
			if(!DATE_FORMAT.test($("#endTime").val())){
				$.scojs_message("您输入的结束日期格式有误，正确格式如：2012-01-01", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		
		if($("#startTime").val()!="" && $("#endTime").val()!=""){
			if($("#startTime").val()>$("#endTime").val()){
				 $.scojs_message("启始日期不能大于结束日期", $.scojs_message.TYPE_ERROR);
				 return false;
			}
		}
		
		search_orderNumber=$("#orderNumber").val();
		search_driverPhone=$("#driverPhone").val();
		search_startTime=$("#startTime").val();
		search_endTime=$("#endTime").val();
		
		var orderTypeArr=[];
		$("input[name=orderType]:checked").each(function(index,element){
			orderTypeArr.push($(element).val());
		});
		if(null!=orderTypeArr && orderTypeArr.length>0){
			search_orderType=orderTypeArr.join(",");
		}
		else{
			search_orderType=[];
		}
		
		offset=0;
		initdata(false);
		
	});
	
	$(document).on("keydown","body",function(){
		if(window.event.keyCode == 13){
			$("#searchButton").click();
		}
	});
	
	//滚动
//	$(window).scroll(function () {
//        //$(window).scrollTop()这个方法是当前滚动条滚动的距离
//        //$(window).height()获取当前窗体的高度
//        //$(document).height()获取当前文档的高度
//        var bot = 0; //bot是底部距离的高度
//        if ((bot + $(window).scrollTop()) >= ($(document).height() - $(window).height())) {
//           //当底部基本距离+滚动的高度〉=文档的高度-窗体的高度时；
//        	if(pageLoading){
//        		pageLoading=false;
//        		offset+=limit;
//        		initdata(true);
//        	}
//        }
//    });
	
	$("#export_button").click(function(){
		
		window.location=ctx+"/driverrefuserecord/exportOrder?"+"&orderNumber="+$("#orderNumber").val()+
		                "&employPhone="+$("#driverPhone").val()+"&orderType="+search_orderType+
		                "&startTime="+$("#startTime").val()+"&endTime="+$("#endTime").val();
	});
	
});


function initdata(isappend){
	
//	showLoading();
	
	$.ajax({
        url : ctx+'/driverrefuserecord/listData',
        type:'GET',
        async:false,
        dataType : 'json',
        data : {
        	"orderNumber":search_orderNumber,
        	"employPhone":search_driverPhone,
        	"startTime":search_startTime,
        	"endTime":search_endTime,
        	"orderType":search_orderType,
        	"pager.offset":offset,
        	"limit":limit,
        },
        success : function(result){
        	var newcontent = '';
        	var totalElements=result.totalElements;
        	$("#NoData").text("");
        	
        	if(totalElements>0){
        		$(result.content).each(function(index,data){
        			var orderNumber=data.orderNumber;
        			var orderType="";
        			
        			if(data.orderType=="daijia"){
        				if(dj){
        					orderType="代驾";
            				orderNumber="<a href='"+ctx+"/order/detail/"+data.orderId+"' class='btn-link'>"+data.orderNumber+"</a>";
            				newcontent+="<tr class='grid-data tr-hui' style='display:none;'>"+
     						"<td>"+orderNumber+"</td>"+
     						"<td>"+data.happend+"</td>"+
     						"<td>"+data.employRealName+"</td>"+
     						"<td>"+data.employPhone+"</td>"+
     						"<td>"+orderType+"</td>"+
     						"<td>"+data.memo+"</td>"+
     					"</tr>";
        				}
        			}
        			else if(data.orderType=="zhuanche"){
        				if(zhuan){
        					orderType="专车";
            				orderNumber="<a href='"+ctx+"/zhuanche/detail/"+data.orderId+"' class='btn-link'>"+data.orderNumber+"</a>";
            				newcontent+="<tr class='grid-data tr-hui' style='display:none;'>"+
     						"<td>"+orderNumber+"</td>"+
     						"<td>"+data.happend+"</td>"+
     						"<td>"+data.employRealName+"</td>"+
     						"<td>"+data.employPhone+"</td>"+
     						"<td>"+orderType+"</td>"+
     						"<td>"+data.memo+"</td>"+
     					"</tr>";
        				}
        			}else if(data.orderType=="paotui"){
        				if(pao){
        					orderType="跑腿";
        					if(data.orderNumber.length == 12){
        						orderNumber="<a href='"+ctx+"/errand/detail/"+data.orderId+"' class='btn-link'>"+data.orderNumber+"</a>";
        					}else{
        						orderNumber="<a href='"+ctx+"/errand/up/detail/"+data.orderId+"' class='btn-link'>"+data.orderNumber+"</a>";
        					}
            				
            				newcontent+="<tr class='grid-data tr-hui' style='display:none;'>"+
     						"<td>"+orderNumber+"</td>"+
     						"<td>"+data.happend+"</td>"+
     						"<td>"+data.employRealName+"</td>"+
     						"<td>"+data.employPhone+"</td>"+
     						"<td>"+orderType+"</td>"+
     						"<td>"+data.memo+"</td>"+
     					"</tr>";
        				}
        			}else if(data.orderType=="freight"){
        				if(freight){
        					orderType="货运";
            				orderNumber="<a href='"+ctx+"/freight/orderDetails/"+data.orderId+"' class='btn-link'>"+data.orderNumber+"</a>";
            				 newcontent+="<tr class='grid-data tr-hui' style='display:none;'>"+
     						"<td>"+orderNumber+"</td>"+
     						"<td>"+data.happend+"</td>"+
     						"<td>"+data.employRealName+"</td>"+
     						"<td>"+data.employPhone+"</td>"+
     						"<td>"+orderType+"</td>"+
     						"<td>"+data.memo+"</td>"+
     					"</tr>";
        				}
        				
        			}else if(data.orderType=="zhuanxian"){
        				if(zhuanxian){
        					orderType="快车";
            				orderNumber="<a href='"+ctx+"/zhuanxian/detail/"+data.orderId+"' class='btn-link'>"+data.orderNumber+"</a>";
            				 newcontent+="<tr class='grid-data tr-hui' style='display:none;'>"+
     						"<td>"+orderNumber+"</td>"+
     						"<td>"+data.happend+"</td>"+
     						"<td>"+data.employRealName+"</td>"+
     						"<td>"+data.employPhone+"</td>"+
     						"<td>"+orderType+"</td>"+
     						"<td>"+data.memo+"</td>"+
     					"</tr>";
        				}
        				
        			}
            	
	   
        		});
        	}else{
              	 $("#NoData").text("当前页面暂无记录 ");
            }

        	if(!isappend){
        		$(".table tbody .grid-data").remove();
        	}
        	$(".table tbody").append(newcontent);
        	
        	$(".table tbody .grid-data").each(function(index,element){
        		$(element).fadeIn(200+(index*35));
        	});
        	
        	if(!hasShowPages){
        		if(!isappend){
            		currentTotalElements=result.content.length;
            		pageLoading=true;
            	}
        		else{
            		if(currentTotalElements<totalElements){
            			currentTotalElements+=result.content.length;
            			pageLoading=true;
            		}
            		else{
            			pageLoading=false;
            		}
            	}
        	}

        	if(hasShowPages){
    			firstLoadPages=true;
        		$(".pageshow li a").html("共"+totalElements+" 条数据");
        		$(".pagedata").paging(totalElements, {
        			format: '[< ncnnn >]',
        			perpage: pagingLimit,
        			page:(offset/limit)+1,
        			onSelect: function (page) {
        				limit=pagingLimit;
        				offset=(page-1)*limit;
        				if(!firstLoadPages){
        					initdata(false);
        				}
        				firstLoadPages=false;
        		    	return false;
        			},
        			onFormat: function (type) {
        				switch (type) {
        				case 'block': // n and c
        					if (!this.active)
        						return '';
        					else if (this.value != this.page)
        						return '<li><a href="#">' + this.value + '</a></li>';
        					return '<li class="active"><a href="#">' + this.value + '</a></li>';
        				case 'next': // >
        					return '<li><a href="#">&rsaquo;</a><li>';
        				case 'prev': // <
        						return '<li><a href="#">&lsaquo;</a><li>';
        				case 'first': // [
        						return '<li><a href="#">&laquo;</a><li>';
        				case 'last': // ]
        						return '<li><a href="#">&raquo;</a></li>';
        				}
        			}
        		});
        		
        	}
        	else{
        		//滚动达到pagingLimit条开始显示分页
//        		if(currentTotalElements==pagingLimit){
            		
            		pageLoading=false;
            		hasShowPages=true;
            		
            		$(".pageshow").show();
            		$(".pagedata").show();
            		$(".pageshow li a").html("共"+totalElements+" 条数据");
            		$(".pagedata").paging(totalElements, {
            			format: '[< ncnnn >]',
            			perpage: pagingLimit,
            			onSelect: function (page) {
            				limit=pagingLimit;
            				offset=(page-1)*limit;
            				if(!firstLoadPages){
            					initdata(false);
            				}
            				firstLoadPages=false;
            		    	return false;
            			},
            			onFormat: function (type) {
            				switch (type) {
            				case 'block': // n and c
            					if (!this.active)
            						return '';
            					else if (this.value != this.page)
            						return '<li><a href="#">' + this.value + '</a></li>';
            					return '<li class="active"><a href="#">' + this.value + '</a></li>';
            				case 'next': // >
            					return '<li><a href="#">&rsaquo;</a><li>';
            				case 'prev': // <
            						return '<li><a href="#">&lsaquo;</a><li>';
            				case 'first': // [
            						return '<li><a href="#">&laquo;</a><li>';
            				case 'last': // ]
            						return '<li><a href="#">&raquo;</a></li>';
            				}
            			}
            		});
            		
//            	}
        	}
        	
        }
	});
	
}

function showLoading(){
	if($('#loadblacklayer').length==0){
		var dw = $(".table").width();
		var dh = $(".table").height();
		var backlayer ="<div id='loadblacklayer'><div style='margin-top:"+(dh/2)+"px;margin-left:"+(dw/2)+"px;'><img src='"+ctx+"/assets/css/images/loading.gif' /></div></div>"
		$(".table tbody").append(backlayer);
		$('#loadblacklayer').css({"z-index":"9","background":"#000","opacity":"0.5","position":'absolute',"top":0,"width":dw,"height":dh});
	}
}

function hideLoading(){
	if($('#loadblacklayer').length>0){
		$('#loadblacklayer').remove();
	}
}

